package com.zf.yichat.config.websocket.client;

import com.zf.yichat.config.EnvConfig;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.Map;

@Slf4j
public class Client extends WebSocketClient {


	private static Client client;
	
	 static {
		try {
			client = new Client(new URI(EnvConfig.getSocketPath()));
			client.connect();
			
		} catch (Exception e) {
			throw new RuntimeException("WebSocketClient init fail..");
		} 
	}
	
	public static Client getInstance() {
		return client;
	}
	

	public Client( URI serverUri , Draft draft ) {
		super( serverUri, draft );
	}

	public Client( URI serverURI ) {
		super( serverURI );
	}

	public Client( URI serverUri, Map<String, String> httpHeaders ) {
		super(serverUri, httpHeaders);
	}

	@Override
	public void onOpen( ServerHandshake handshakedata ) {
		log.info( "opened connection" );
	}

	@Override
	public void onMessage( String message ) {
		log.info( "received: " + message );
	}

	@Override
	public void onClose( int code, String reason, boolean remote ) {
		log.info( "Connection closed by " + ( remote ? "remote peer" : "us" ) + " Code: " + code + " Reason: " + reason );
	}

	@Override
	public void onError( Exception ex ) {
		log.error(ex.getMessage(), ex);
	}
}
